package core;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.List;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.VolatileImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import HECS.HECSEntity;
import HECS.HECSManager;
import components.Player;
import components.GridPositionComponent;
import core.Utility;
import core.creators.units.PlayerCreator;

/**
 * @author Marcus
 *
 */
public class Main implements KeyListener, MouseListener {
	HECSManager ecsManagar = new HECSManager();
	public Window window;
	public int xpos;
	public int ypos;
	public int zpos;
	public Random random;
	public HECSEntity player;
	Button button_north = new Button();// north
	Button button_west = new Button(30); // west
	Button button_east = new Button(60); // east
	Button button_south = new Button(90); // south

	public static void main(String[] args) {
		int[] derp = new int[4];
		new Main();
	}

	public Main() {
		map = new HashMap<Integer, HashMap<Integer, HashMap<Integer, GridPositionComponent>>>();
		random = new Random();
		window = new Window();
		window.addKeyListener(this);
		window.addMouseListener(this);
		Graphics2D g = null;
		player = PlayerCreator.spawnPlayer(ecsManagar);

		draw();
		// prints a list of coordinates of all adventures
		// for (Entry<Integer, HashMap<Integer, HashMap<Integer, Adventure>>> entryZ : map.entrySet()) {
		// for (Entry<Integer, HashMap<Integer, Adventure>> entryX : entryZ.getValue().entrySet()) {
		// for (Entry<Integer, Adventure> entryY : entryX.getValue().entrySet()) {
		// System.out.println(entryY.getValue().coordinatesToString());
		// }}}

	}

	private void renderButtons(Graphics2D g) {

		button_north.render(g);
		button_west.render(g);
		button_east.render(g);
		button_south.render(g);
	}

	private void move(int x, int y, int z) {
		player.posx += x;
		player.posy += y;
		player.posz += z;
		GridManager.generateTileZone(player.posx, player.posy, player.posz, map);

		GridPositionComponent tile_east = GridManager.findTile(x + 1, y, z, map); // east
		GridPositionComponent tile_north = GridManager.findTile(x, y + 1, z, map); // north
		GridPositionComponent tile_west = GridManager.findTile(x - 1, y, z, map); // west
		GridPositionComponent tile_south = GridManager.findTile(x, y - 1, z, map); // south
		if (player.navigation) {
			button_east.setDescriptions(tile_east, "east"); // east
			button_north.setDescriptions(tile_north, "north"); // north
			button_west.setDescriptions(tile_west, "west"); // west
			button_south.setDescriptions(tile_south, "south"); // south
		} else {
			ArrayList<GridPositionComponent> list = new ArrayList<GridPositionComponent>();
			GridPositionComponent tile;
			list.add(tile_east);
			list.add(tile_north);
			list.add(tile_west);
			list.add(tile_south);
			tile = list.get(Utility.rndInt(list.size()));
			list.remove(tile);
			button_north.setDescriptions(tile, "1"); // north

			tile = list.get(Utility.rndInt(list.size()));
			list.remove(tile);
			button_west.setDescriptions(tile, "2"); // west

			tile = list.get(Utility.rndInt(list.size()));
			list.remove(tile);
			button_east.setDescriptions(tile, "3"); // east

			tile = list.get(Utility.rndInt(list.size()));
			list.remove(tile);
			button_south.setDescriptions(tile, "4"); // south
		}
		draw();
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == e.VK_W)
			move(0, 1, 0);
		if (e.getKeyCode() == e.VK_S)
			move(0, -1, 0);
		if (e.getKeyCode() == e.VK_A)
			move(-1, 0, 0);
		if (e.getKeyCode() == e.VK_D)
			move(1, 0, 0);
	}

	public void keyReleased(KeyEvent e) {
	}

	public void keyTyped(KeyEvent e) {

	}

	public void render() {
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();

		Graphics2D g = null;
		do {
			int valid = window._vimage.validate(gc);
			if (valid == VolatileImage.IMAGE_INCOMPATIBLE) {
				window._vimage = window.createVolatileImage();
			}
			try {
				g = window._vimage.createGraphics();
				g.setColor(Color.BLACK);
				g.fillRect(0, 0, window.getWidth(), window.getHeight());
				i++;
				GridManager.render(player.posx, player.posy, player.posz, map, g);
				renderButtons(g);
			} finally {
				g.dispose();
			}
		} while (window._vimage.contentsLost());
	}

	public void draw() {
		Graphics2D g = (Graphics2D) window.getGraphics();
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
		window._vimage = window.createVolatileImage();
		render();
		g.drawImage(window._vimage, 0, 0, null);
	}

}